import numpy as np

import sys, os
sys.path.append('src/')
import transceiver
import misc


fftlen = 1024
Fs = fftlen/(1./15000)

RB = 50
CP = True
modsym = 7


ps = misc.PulseShaping(Fs)
cc = misc.Calc()
dh = misc.DataHandling()

bb = transceiver.Baseband(RB,fftlen)
fco = 5E6

""" Simulation of SC-FDM (OFDM is outcommented) """
x16qam = np.random.randint(0,2,48*RB*modsym)
#xmod,xtemp = bb.ofdm(x16qam,modsym,CP,'16QAM',modout=True,shift=True)
xmod1,xtemp1 = bb.sc_fdm(x16qam,modsym,CP,'16QAM',modout=True,shift=True)

#xps = ps.rect(xmod,fco)
xps1 = ps.rect(xmod1,fco)

#ydem = bb.ofdm_dem(xps,modsym,CP)
ydem1 = bb.sc_fdm_dem(xps1,modsym,CP)


""" Arange data correctly for plotting """
ff = np.fft.fftfreq(len(xps1), 1./Fs)
ff1 = list(ff)[len(ff)/2::]+list(ff)[0:len(ff)/2]

xps11 = 10*np.log10(abs(np.fft.fft(xps1)))
xmod11 = 10*np.log10(abs(np.fft.fft(xmod1)))

xps12 = list(xps11)[len(xps11)/2::]+list(xps11)[0:len(xps11)/2]
xmod12 = list(xmod11)[len(xmod11)/2::]+list(xmod11)[0:len(xmod11)/2]

dh.write_data(ff1[0::4],xps12[0::4],'tex/data/spectrum_with.txt')
dh.write_data(ff1[0::4],xmod12[0::4],'tex/data/spectrum_without.txt')
